#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Solution {
    class greater {
    public:
        bool operator()(const vector<int>& a, const vector<int>& b) {
            if (a[0] < b[0]) {
                return true;
            }
            else if (a[0] > b[0]) {
                return false;
            }
            else {
                if (a[1] < b[1]) {
                    return true;
                }
                else if (a[1] > b[1]) {
                    return false;
                }
            }
            return false;
        }
    };

public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if (intervals.size() == 0)return {};
        sort(intervals.begin(), intervals.end(), greater());
        // for(auto e : intervals){
        //     cout << e[0] << " " << e[1] << endl;
        // }
        vector<vector<int>> ans;
        ans.push_back({ intervals[0][0], intervals[0][1] });
        int len = intervals.size();
        for (int i = 1; i < len; i++) {
            if (intervals[i][0] <= ans.back()[1]) {
                ans.back()[0] = min(intervals[i][0], ans.back()[0]);
                ans.back()[1] = max(intervals[i][1], ans.back()[1]);
            }
            else ans.push_back({ intervals[i][0], intervals[i][1] });
        }
        return ans;
    }
};